<?php

use Chaosyeo\SolarEngine\Event;
use Chaosyeo\SolarEngine\Properties;
use Chaosyeo\SolarEngine\RestfulClient;
use Chaosyeo\SolarEngine\User\User as User;

require __DIR__ . '/../vendor/autoload.php';

$key    = 'd6da8fe637382e8c';

$client = new RestfulClient($key, [
    'host' => RestfulClient::DEFAULT_OVERSEA_HOST
]);

/**
 * 事件上报示例
 * 参考文档: 参考文档: https://help.solar-engine.com/cn/docs/Koumhl
 */


// 构造基本用户及设备数据
$distinctId  = '13ae521aee2ce1c0';
$androidId   = '13ae521aee2ce1c0';
$gaid        = '11e085e6-d2ca-4594-9fad-f67942d8268f';
$ua          = 'Dalvik/2.1.0 (Linux; U; Android 14; SM-A556E Build/UP1A.231005.007)';
$ip          = '1.119.175.47';
$accountId   = '6931978';
$visitorId   = '13ae521aee2ce1c0';

// 构造应用平台数据
$platform    = Event\Properties::PLATFORM_ANDROID;
$packageName = 'com.lfkbonustdg.abhdsahgdw';
$packageType = Event\Event::PACKAGE_TYPE_OVERSEA;
$appType     = Event\Event::APP_TYPE_APP;

// 构造用户数据结构
$user = new Event\User($distinctId, $ua, $ip, $accountId, $visitorId, [
    'gaid'      => $gaid,
    'androidId' => $androidId,
]);


 print_r('[DEBUG] PREPARING LOGIN EVENT...' . PHP_EOL);
 // 构造登录事件属性
 $loginType  = 'mobile';
 $properties = new Event\LoginProperties($platform, $loginType, ['packageName' => $packageName]);
 // 构造登录事件
 $eventName = Event\Event::EVENT_NAME_PREDEF_LOGIN;
 $event     = new Event\Event($packageType, $user, $properties, $appType, $eventName, []);
 print_r('[DEBUG] LOGIN EVENT CONTENT ' . json_encode($event->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$event]);
 print_r('[DEBUG] LOGIN EVENT REPORTED.' . PHP_EOL);

 // 构造注册事件属性
 print_r('[DEBUG] PREPARING REGISTRATION EVENT...' . PHP_EOL);
 $regType    = 'mobile';
 $properties = new Event\RegistrationProperties($platform, $regType, ['packageName' => $packageName]);
 // 构造注册事件
 $eventName = Event\Event::EVENT_NAME_PREDEF_REGISTRATION;
 $event     = new Event\Event($packageType, $user, $properties, $appType, $eventName, []);
 print_r('[DEBUG] LOGIN EVENT CONTENT ' . json_encode($event->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 // $client->send([$event]);
 print_r('[DEBUG] LOGIN EVENT REPORTED.' . PHP_EOL);

 // 构造订单事件属性
 print_r('[DEBUG] PREPARING ORDER EVENT...' . PHP_EOL);
 $payAmount    = 100.00;
 $currencyType = 'INR';
 $orderId      = '20250306.6931485.1';
 $properties = new Event\OrderProperties($platform, $payAmount, $currencyType, [
     'packageName' => $packageName,
     'orderId'     => $orderId
 ]);
 // 构造订单事件
 $eventName = Event\Event::EVENT_NAME_PREDEF_ORDER;
 $event     = new Event\Event($packageType, $user, $properties, $appType, $eventName, []);
 print_r('[DEBUG] ORDER EVENT CONTENT ' . json_encode($event->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 // $client->send([$event]);
 print_r('[DEBUG] ORDER EVENT REPORTED.' . PHP_EOL);

 // 构造支付事件属性
 print_r('[DEBUG] PREPARING PURCHASE EVENT...' . PHP_EOL);
 $payStatus  = Event\PurchaseProperties::PAY_STATUS_SUCCESS;
 $properties = new Event\PurchaseProperties($platform, $payAmount, $currencyType, $payStatus, [
     'packageName' => $packageName,
     'orderId'     => $orderId
 ]);
 // 构造支付事件
 $eventName = Event\Event::EVENT_NAME_PREDEF_PURCHASE;
 $event     = new Event\Event($packageType, $user, $properties, $appType, $eventName, []);
 print_r('[DEBUG] PURCHASE EVENT CONTENT ' . json_encode($event->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 // $client->send([$event]);
 print_r('[DEBUG] PURCHASE EVENT REPORTED.' . PHP_EOL);

/**
 * 用户属性上报示例
 * 参考文档: https://help.solar-engine.com/cn/docs/Koumhl#8318161eabc670e67027f46c17ec3f2b
 */

$properties = new Properties([
    'creation_date' => '2025-03-06'
]);

$user = new User($packageType, User::USERSET_TYPE_INIT, $accountId, $visitorId, $properties, [
    'gaid'      => $gaid,
    'androidId' => $androidId,
    'ua'        => $ua,
    'ip'        => $ip,
]);

 // 如果您要上传一批用户属性，其中已经存在的用户属性不去更新属性值、不存在的属性进行创建并保存属性值，则可以调用 userInit 来进行设置。
 print_r('[DEBUG] PREPARING USERSET INIT...' . PHP_EOL);
 $user->setUsersetType(User::USERSET_TYPE_INIT);
 print_r('[DEBUG] USERSET INIT CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$user]);
 print_r('[DEBUG] USERSET INIT REPORTED.' . PHP_EOL);

 // 对于一般的用户属性，您可以调用 userUpdate 来进行设置，使用该接口上传的属性将会覆盖原有的属性值，如果之前不存在该用户属性，则会新建该用户属性，类型与传入属性的类型一致。
 print_r('[DEBUG] PREPARING USERSET UPDATE...' . PHP_EOL);
 $user->setUsersetType(User::USERSET_TYPE_UPDATE);
 $user->properties->setCustomProperties([
     'creation_date'     => '2025-03-06',
     'registration_date' => '2025-03-06',
 ]);
 print_r('[DEBUG] USERSET UPDATE CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$user]);
 print_r('[DEBUG] USERSET UPDATE REPORTED.' . PHP_EOL);

 // 当您要上传数值型的属性时，您可以调用 userAdd 来对该属性进行累加操作，如果该属性还未被设置，则会赋值 0 后再进行计算，可传入负值，等同于相减操作。
 print_r('[DEBUG] PREPARING USERSET ADD...' . PHP_EOL);
 $user->setUsersetType(User::USERSET_TYPE_ADD);
 $user->properties->setCustomProperties([
     'deposit_count'    => 1,
     'deposit_coin_sum' => 100.00,
 ]);
 print_r('[DEBUG] USERSET ADD CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$user]);
 print_r('[DEBUG] USERSET ADD REPORTED.' . PHP_EOL);

 // 当您要清空用户的用户属性值时，您可以调用 userUnset 来对指定属性（字符串数组）进行清空操作，如果该属性还未在集群中被创建，则 不会 创建该属性。
 print_r('[DEBUG] PREPARING USERSET UNSET...' . PHP_EOL);
 $user->setUsersetType(User::USERSET_TYPE_UNSET);
 $user->properties->setCustomProperties([
     'tags' => []
 ]);
 print_r('[DEBUG] USERSET UNSET CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$user]);
 print_r('[DEBUG] USERSET UNSET REPORTED.' . PHP_EOL);

 // 您可以调用 userAppend 对数组类型的用户属性进行追加操作。如果该属性不存在，则会新建该属性。
 print_r('[DEBUG] PREPARING USERSET APPEND...' . PHP_EOL);
 $user->setUsersetType(User::USERSET_TYPE_APPEND);
 $user->properties->setCustomProperties([
     'tags' => ['A'],
 ]);
 print_r('[DEBUG] USERSET APPEND CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
 $client->send([$user]);
 print_r('[DEBUG] USERSET APPEND REPORTED.' . PHP_EOL);


// 如果您要删除某个用户，可以调用 userDelete 将这名用户删除。用户删除后，您将无法再查询该名用户的用户属性，但该用户产生的事件仍然可以被查询到。
print_r('[DEBUG] PREPARING USERSET DELETE...' . PHP_EOL);
$user->setUsersetType(User::USERSET_TYPE_DELETE_BY_ACCOUNT_ID);
$user->properties->setCustomProperties([
    'tags'              => ['A'],
    'registration_date' => '2025-03-06'
]);
print_r('[DEBUG] USERSET DELETE CONTENT ' . json_encode($user->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL);
$client->send([$user]);
print_r('[DEBUG] USERSET DELETE REPORTED.' . PHP_EOL);